package cn.changshengjue.dakong.utils;

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.jar.*;

import org.bukkit.inventory.*;

import cn.changshengjue.dakong.main;

import java.lang.reflect.*;

public class NbtHandler
{
    private String version;
    
    public NbtHandler() {
        this.version = this.getTureVersion();
        if (this.version == null) {
            main.getInstance().startFail = true;
        }
    }

    private String getTureVersion() {
        try {
            String pack = "net.minecraft.server.";
            pack = pack.replace('.', '/');
            final Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources(pack);
            while (urls.hasMoreElements()) {
                final URL url = urls.nextElement();
                final JarFile jar = ((JarURLConnection)url.openConnection()).getJarFile();
                final Enumeration<JarEntry> entries = jar.entries();
                while (entries.hasMoreElements()) {
                    final JarEntry entry = entries.nextElement();
                    String name = entry.getName();
                    if (name.startsWith(pack) && name.contains(".class")) {
                        name = name.replaceAll(pack, "");
                        final String[] raws = name.split("/");
                        return raws[0];
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }




    /**
     * 获取NMS对象
     * @return
     *
     * net.minecraft.server.v1_12_R1.ItemStack nmsItem = CraftItemStack.asNMSCopy(item);
     *
     */
    public Class<?> getItemClass() {
        try {
            return Class.forName("net.minecraft.server." + this.version + ".ItemStack");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }


    public Class<?> getNBTTagCompoundClass() {
        try {
            return Class.forName("net.minecraft.server." + this.version + ".NBTTagCompound");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取NBT里面的NBTTagCompound实例
     * @return
     */
    public Object getNBTTagCompoundInstance() {
        try {
            final Class<?> clazz = Class.forName("net.minecraft.server." + this.version + ".NBTTagCompound");
            return clazz.newInstance();
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex2) {
            final ReflectiveOperationException ex = null;
            final ReflectiveOperationException e = ex;
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 获取NBT里面的NBTReadLimiter
     * @return
     */
    public Class<?> getNBTReadLimiterClass() {
        try {
            return Class.forName("net.minecraft.server." + this.version + ".NBTReadLimiter");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Object getNBTReadLimiter() {
        try {
            final Class<?> clazz = Class.forName("net.minecraft.server." + this.version + ".NBTReadLimiter");
            final Field field = clazz.getField("a");
            return field.get(null);
        }
        catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex2) {
            final Exception ex = null;
            final Exception e = ex;
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 获取NBT里面的  CraftItemStack
     * @return
     */
    public Class<?> getCraftItemStackClass() {
        try {
            return Class.forName("org.bukkit.craftbukkit." + this.version + ".inventory.CraftItemStack");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Object asCraftMirror(final Object object) {
        try {
            final Class<?> clazz = Class.forName("org.bukkit.craftbukkit." + this.version + ".inventory.CraftItemStack");
            final Method method = clazz.getMethod("asCraftMirror", this.getItemClass());
            return method.invoke(null, object);
        }
        catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex2) {
            final Exception ex = null;
            final Exception e = ex;
            e.printStackTrace();
            return null;
        }
    }

    public Object asCraftCopy(final Object object) {
        try {
            final Class<?> clazz = Class.forName("org.bukkit.craftbukkit." + this.version + ".inventory.CraftItemStack");
            final Method method = clazz.getMethod("asCraftCopy", ItemStack.class);
            return method.invoke(null, object);
        }
        catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex2) {
            final Exception ex = null;
            final Exception e = ex;
            e.printStackTrace();
            return null;
        }
    }

    public Object getItemStack(final Object object) {
        if (this.isHighVersion()) {
            return this.getNewItemStack(object);
        }
        return this.getCreateItemStack(object);
    }

    private boolean isHighVersion() {
        final String[] raws = this.version.split("_");
        final String m = raws[1];
        final int n = Integer.parseInt(m);
        return n >= 11;
    }


    private Object getNewItemStack(final Object object) {
        try {
            final Class<?> clazz = Class.forName("net.minecraft.server." + this.version + ".ItemStack");
            final Constructor<?> cons = clazz.getConstructor(this.getNBTTagCompoundClass());
            return cons.newInstance(object);
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex2) {
            final Exception ex = null;
            final Exception e = ex;
            e.printStackTrace();
            return null;
        }
    }

    private Object getCreateItemStack(final Object object) {
        try {
            final Class<?> clazz = Class.forName("net.minecraft.server." + this.version + ".ItemStack");
            final Method method = clazz.getMethod("createStack", this.getNBTTagCompoundClass());
            return method.invoke(null, object);
        }
        catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex2) {
            final Exception ex = null;
            final Exception e = ex;
            e.printStackTrace();
            return null;
        }
    }

    public Class<?> getItemSatckClass() {
        try {
            return Class.forName("net.minecraft.server." + this.version + ".ItemStack");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
